package com.kaibes.imserver.model.utils;

import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONException;
import org.json.JSONObject;

public class StaticValuesUtil {

	public final static String DATA = "data";
	public final static String USER = "user";
	public final static String CACHE = "cache";
	public final static String FILES = "files";
	public final static String PHOTO = "photo";

	public final static String ROOTFILE = DATA + File.separator + USER;
	public final static String ROOTFILE_CACHE = DATA + File.separator + USER
			+ File.separator + CACHE;
	public final static String ROOTFILE_FILES = DATA + File.separator + USER
			+ File.separator + FILES;

	// type
	public final static String USER_PHOTO = "user_photo";

	public final static HashMap<String, Integer> faceHashClassic = new HashMap<String, Integer>();

	public final static ArrayList<String> foldersContact = new ArrayList<String>();
	public final static ArrayList<String> foldersUSer = new ArrayList<String>();

	public static boolean isFilename(String filename){
		if (filename.contains("/")||filename.contains("\\")||filename.contains("..")) {
			return false;
		}
		return true;
	}
	
	public static boolean isExistsUserFolder(String filefolder) {
		return foldersUSer.contains(filefolder);
	}

	public static boolean isExistsContactFolder(String filefolder) {
		return foldersContact.contains(filefolder);
	}

	public static String getType(String filefolder) {
		String turn = "";
		if (isExistsUserFolder(filefolder)) {
			switch (filefolder) {
			case StaticValuesUtil.PHOTO:
				turn = StaticValuesUtil.USER_PHOTO;
				break;

			default:
				break;
			}
		}
		return turn;
	}
	
	public static String changeStringForRegexp(String str){
		if (str.equals("")) {
			return ".*";
		}
		return str;
	}
	
	public static String changeSexForRegexp(String str){
		if (str.equals("男")||str.equals("女")) {
			return str;
		}
		return ".*";
	}
	
	public static String changeBirthForRegexp_1(String str){
		Pattern pattern = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})");
		Matcher matcher = pattern.matcher(str);
		
		if (str.equals("")||(!matcher.matches())) {
			str = "1900-01-01";
		}
		
		return str;
	}
	
	public static String changeBirthForRegexp_2(String str){
		Pattern pattern = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})");
		Matcher matcher = pattern.matcher(str);
		
		if (str.equals("")||(!matcher.matches())) {
			str = "2100-01-01";
		}
		
		return str;
	}
	
	public static JSONObject result2jsonObjecContact(ResultSet result){
		JSONObject jsonObject =  result2jsonObjec(result);
		try {
			jsonObject.put("contactname", result.getString("username"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonObject;
	}
	
	public static JSONObject result2jsonObjecUser(ResultSet result){
		JSONObject jsonObject =  result2jsonObjec(result);
		try {
			jsonObject.put("username", result.getString("username"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonObject;
	}
	
	private static JSONObject result2jsonObjec(ResultSet result){
		JSONObject jsonObject = new JSONObject();
		
		if (result != null) {
			try {
				jsonObject.put("nickname", result.getString("nickname"));
				jsonObject.put("photo", result.getString("photo"));
				jsonObject.put("sex", result.getString("sex"));
				jsonObject.put("birth", result.getString("birth"));
				jsonObject.put("signature", result.getString("signature"));
				jsonObject.put("introduction", result.getString("introduction"));
				jsonObject.put("job", result.getString("job"));
				jsonObject.put("company", result.getString("company"));
				jsonObject.put("school", result.getString("school"));
				jsonObject.put("address", result.getString("address"));
				jsonObject.put("hometown", result.getString("hometown"));
				jsonObject.put("email", result.getString("email"));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return jsonObject;
	}

	static {

		foldersContact.add(PHOTO);
		foldersUSer.add(PHOTO);

		faceHashClassic.put("face_delete", 1);
		faceHashClassic.put("face_qq_0", 1);
		faceHashClassic.put("face_qq_1", 1);
		faceHashClassic.put("face_qq_2", 1);
		faceHashClassic.put("face_qq_3", 1);
		faceHashClassic.put("face_qq_4", 1);
		faceHashClassic.put("face_qq_5", 1);
		faceHashClassic.put("face_qq_6", 1);
		faceHashClassic.put("face_qq_7", 1);
		faceHashClassic.put("face_qq_8", 1);
		faceHashClassic.put("face_qq_9", 1);
		faceHashClassic.put("face_qq_10", 1);
		faceHashClassic.put("face_qq_11", 1);
		faceHashClassic.put("face_qq_12", 1);
		faceHashClassic.put("face_qq_13", 1);
		faceHashClassic.put("face_qq_14", 1);
		faceHashClassic.put("face_qq_15", 1);
		faceHashClassic.put("face_qq_16", 1);
		faceHashClassic.put("face_qq_17", 1);
		faceHashClassic.put("face_qq_18", 1);
		faceHashClassic.put("face_qq_19", 1);
		faceHashClassic.put("face_qq_20", 1);
		faceHashClassic.put("face_qq_21", 1);
		faceHashClassic.put("face_qq_22", 1);
		faceHashClassic.put("face_qq_23", 1);
		faceHashClassic.put("face_qq_24", 1);
		faceHashClassic.put("face_qq_25", 1);
		faceHashClassic.put("face_qq_26", 1);
		faceHashClassic.put("face_qq_27", 1);
		faceHashClassic.put("face_qq_28", 1);
		faceHashClassic.put("face_qq_29", 1);
		faceHashClassic.put("face_qq_30", 1);
		faceHashClassic.put("face_qq_31", 1);
		faceHashClassic.put("face_qq_32", 1);
		faceHashClassic.put("face_qq_33", 1);
		faceHashClassic.put("face_qq_34", 1);
		faceHashClassic.put("face_qq_35", 1);
		faceHashClassic.put("face_qq_36", 1);
		faceHashClassic.put("face_qq_37", 1);
		faceHashClassic.put("face_qq_38", 1);
		faceHashClassic.put("face_qq_39", 1);
		faceHashClassic.put("face_qq_40", 1);
		faceHashClassic.put("face_qq_41", 1);
		faceHashClassic.put("face_qq_42", 1);
		faceHashClassic.put("face_qq_43", 1);
		faceHashClassic.put("face_qq_44", 1);
		faceHashClassic.put("face_qq_45", 1);
		faceHashClassic.put("face_qq_46", 1);
		faceHashClassic.put("face_qq_47", 1);
		faceHashClassic.put("face_qq_48", 1);
		faceHashClassic.put("face_qq_49", 1);
		faceHashClassic.put("face_qq_40", 1);
		faceHashClassic.put("face_qq_41", 1);
		faceHashClassic.put("face_qq_42", 1);
		faceHashClassic.put("face_qq_43", 1);
		faceHashClassic.put("face_qq_44", 1);
		faceHashClassic.put("face_qq_45", 1);
		faceHashClassic.put("face_qq_46", 1);
		faceHashClassic.put("face_qq_47", 1);
		faceHashClassic.put("face_qq_48", 1);
		faceHashClassic.put("face_qq_49", 1);
		faceHashClassic.put("face_qq_50", 1);
		faceHashClassic.put("face_qq_51", 1);
		faceHashClassic.put("face_qq_52", 1);
		faceHashClassic.put("face_qq_53", 1);
		faceHashClassic.put("face_qq_54", 1);
		faceHashClassic.put("face_qq_55", 1);
		faceHashClassic.put("face_qq_56", 1);
		faceHashClassic.put("face_qq_57", 1);
		faceHashClassic.put("face_qq_58", 1);
		faceHashClassic.put("face_qq_59", 1);
		faceHashClassic.put("face_qq_60", 1);
		faceHashClassic.put("face_qq_61", 1);
		faceHashClassic.put("face_qq_62", 1);
		faceHashClassic.put("face_qq_63", 1);
		faceHashClassic.put("face_qq_64", 1);
		faceHashClassic.put("face_qq_65", 1);
		faceHashClassic.put("face_qq_66", 1);
		faceHashClassic.put("face_qq_67", 1);
		faceHashClassic.put("face_qq_68", 1);
		faceHashClassic.put("face_qq_69", 1);
		faceHashClassic.put("face_qq_70", 1);
		faceHashClassic.put("face_qq_71", 1);
		faceHashClassic.put("face_qq_72", 1);
		faceHashClassic.put("face_qq_73", 1);
		faceHashClassic.put("face_qq_74", 1);
		faceHashClassic.put("face_qq_75", 1);
		faceHashClassic.put("face_qq_76", 1);
		faceHashClassic.put("face_qq_77", 1);
		faceHashClassic.put("face_qq_78", 1);
		faceHashClassic.put("face_qq_79", 1);
	}

}
